package com.auxgroup.hr.front.commons.utils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Table;

import org.apache.commons.lang.StringUtils;

import com.auxgroup.hr.front.commons.vo.UserDTO;

/**
 * @author lvliuhao
 * @version 创建时间：2018年5月29日 下午4:41:50 类字段工具类
 */
public class ObjectFieldUtils {

	/**
	 * 根据属性名获取属性值
	 * */
	public static Object getFieldValueByName(String fieldName, Object o) {
		Field field = null;
		Class<?> objClass = o.getClass();
		while (field==null) {
			try {
				field = objClass.getDeclaredField(fieldName);
			} catch (Exception e) {
				objClass = objClass.getSuperclass();
				if(objClass == Object.class){
					break;
				}
			}
		}
		if(field==null){
			return null;
		}
		try {
			field.setAccessible(true);//设置字段可访问，即暴力反射
			return field.get(o);
		} catch (Exception e) {
			return null;
		} 
	}
	
	/**
	 * 根据属性名设置属性值
	 * */
	public static Boolean setFieldValueByName(String fieldName, Object o,Object fieldValue) {
		Field field = null;
		Class<?> objClass = o.getClass();
		while (field==null) {
			try {
				field = objClass.getDeclaredField(fieldName);
			} catch (Exception e) {
				objClass = objClass.getSuperclass();
				if(objClass == Object.class){
					break;
				}
			}
		}
		if(field==null){
			return false;
		}
		try {
			field.setAccessible(true);//设置字段可访问，即暴力反射
			field.set(o, fieldValue);
			return true;
		} catch (Exception e) {
			return false;
		} 
	}

	/**
	 * 获取属性名数组
	 * */
	public static String[] getFiledName(Object o) {
		List<Field> fields = new ArrayList<Field>();
		Class<?> tempClass = o.getClass();
		while (tempClass != null) {// 当父类为null的时候说明到达了最上层的父类(Object类).
			fields.addAll(Arrays.asList(tempClass.getDeclaredFields()));
			tempClass = tempClass.getSuperclass(); // 得到父类,然后赋给自己
		}
		for (int i = 0; i < fields.size(); i++) {
			if("serialVersionUID".equals(fields.get(i).getName())){
				fields.remove(i);
			}
		}
		String[] fieldNames = new String[fields.size()];
		for (int i = 0; i < fields.size(); i++) {
			fieldNames[i] = fields.get(i).getName();
		}
		return fieldNames;
	}

	/**
	 * 获取对象的所有属性值，返回一个对象数组
	 * */
	public static Object[] getFiledValues(Object o) {
		String[] fieldNames = getFiledName(o);
		Object[] value = new Object[fieldNames.length];
		for (int i = 0; i < fieldNames.length; i++) {
			value[i] = getFieldValueByName(fieldNames[i], o);
		}
		return value;
	}

	/**
	 * 获取 entity对象映射的表名字
	 * 
	 * @param o
	 * @return
	 */
	public static String getEntityTableName(Object o) {
		Table tableAnno = o.getClass().getAnnotation(Table.class);
		if (tableAnno != null) {
			return tableAnno.name();
		}
		return null;
	}

	/**
	 * 处理属性为空的字段 设置为" "
	 * 
	 * @param o
	 */
	public static void handleNullValue(Object o, Set<String> ignoreFields) {
		if (o == null) {
			return;
		}
		try {
			List<Field> fields = new ArrayList<Field>();
			Class<?> tempClass = o.getClass();
			while (tempClass != null) {//当父类为null的时候说明到达了最上层的父类(Object类).
				fields.addAll(Arrays.asList(tempClass.getDeclaredFields()));
				tempClass = tempClass.getSuperclass(); //得到父类,然后赋给自己
			}
			for (Field field : fields) {
				if (!ignoreFields.contains(field.getName())) {
					field.setAccessible(true);
					if (field.get(o) != null
							&& StringUtils.isNotBlank(field.get(o).toString())) {
						continue;
					}
					if (field.getType() == String.class) {
						field.set(o, " ");// 动态设置值
						continue;
					}
//					if (field.getType() == Date.class) {
//						field.set(o, new Date());// 动态设置值
//						continue;
//					}
//					if (field.getType() == Timestamp.class) {
//						field.set(o, new Timestamp(System.currentTimeMillis()));// 动态设置值
//						continue;
//					}
					if (field.getType() == BigDecimal.class) {
						field.set(o, new BigDecimal(0));// 动态设置值
						continue;
					}
					if (field.getType() == Integer.class) {
						field.set(o, 0);// 动态设置值
						continue;
					}
				}
			}
		} catch (Exception e) {
		}
	}

	/**
	 * 处理属性为空的字段 设置为" "
	 * 
	 * @param o
	 */
	public static void handleNullValue(Object o) {
		handleNullValue(o, new HashSet<String>());
	}
	
	
	
	public static void main(String args[]) {
		UserDTO user = new UserDTO();
		
		System.out.println("---"+getFieldValueByName("oprId11", user));
		
	}

		
		
		
	
}
